<?php
include '../include/common.inc.php';
include './include/common.inc.php';
include '../include/type.class.php';

$typeobj = new type();

$action = isset($action) ? $action : 'manage';
$parentcode = isset($parentcode) ? $parentcode : '';
$topcode = isset($topcode) ? $topcode : '';
$forward = get_forward('admin/type.php');
switch ($action)
{
    case 'add':
            $have_perm = check_perms('TYPE002');

            if($have_perm == false)
            {
                showmessage('您没有权限进行此操作！');
                $response->execute();
            }
            if(isset($type))
            {
                if(empty($type['code']) || strlen($type['code']) > 30)
                {
                    $response->text_alert('error', 'code', 'code不能为空且长度不能大于30个字符');
                }
                $where = "tcode = '".$type['code']."'";
                $res = $typeobj->get_one($where);
                if($res)
                {
                    $response->text_alert('error', 'code', 'code已存在');
                }
                if(empty($type['name']) || strlen($type['name']) > 200)
                {
                    $response->text_alert('error', 'name', '名称不能为空且长度不能大于200个字符');
                }
/*
 *  判断名称不能重复代码
 */
//                $where = "tname = '".$type['name']."'";
//                $res = $typeobj->get_one($where);
//                if($res)
//                {
//                    $response->text_alert('error', 'name', '名称已存在');
//                }
                $response->real_execute();
                $type['inputtime'] = TIME;
/*
* 判断录入的topcode值
*/
                if($type['topcode'] != '')
                {
                    $type['topcode'] = $type['topcode'];
                }
                elseif($type['pcode']=='' && $type['topcode'] =='')
                {
                    $type['topcode'] = '';
                }
                else
                {
                    $type['topcode'] = $type['pcode'];
                }


                $res = $typeobj->add($type);
                if(res === false)
                {
                  $response->alert('添加失败');
                  $response->script('self.location=self.location');
                }
                else
                {
                  $response->alert('添加成功');
                  $response->script("javascript:history.go(-1)");
                }
                $response->execute();
            }
            else
            {
//$options 所有分类
                $options = $typeobj->alltype();
                include template('admin', 'type_add');
            }
//            }
        break;
    case 'edit':
        $have_perm = check_perms('TYPE003');
        if($have_perm == false)
        {
            showmessage('您没有权限进行此操作！');
        }
        $tid = isset($tid) ? $tid : '';
        if(isset($type) && $tid != '')
        {
            if(empty($type['code']) || strlen($type['code']) > 30)
            {
                $response->text_alert('error', 'code', 'code不能为空且长度不能大于30个字符');
            }
              $where = "tcode = '".$type['code']."' and tid != '".$tid."'";
              $res = $typeobj->get_one($where);
            if($res)
            {
                $response->text_alert('error', 'code', 'code已存在');
            }
            $l = strlen($type['name']);
            if(empty($type['name']) || strlen($type['name']) > 200)
            {
                $response->text_alert('error', 'name', '名称不能为空且长度不能大于200个字符');
            }
/*
 *   判断 名称是否可以重复代码
 */
//              $where = "tname = '".$type['name']."' and tid != '".$tid."'";
//              $res = $typeobj->get_one($where);
//            if($res)
//            {
//                $response->text_alert('error', 'name', '名称已存在');
//            }
            $response->real_execute();
//获取父类的topcode
            $where = "tcode = '".$type['pcode']."'";
            $res = $typeobj->get_one($where);
            if($res['topcode'] != '')
            {
                $type['topcode'] = $res['topcode'];
            }
            elseif($res['tcode'] == '')
            {
                $type['topcode'] = '';
            }
            else
            {
                $type['topcode'] = $res['tcode'];
            }
            $res = $typeobj->edit($type, $tid);
            $res_return = $typeobj->change_chl_topcode($type['firstcode'],$type['code'],$type['topcode']);
            if($res_return === false)
            {
                showmessage('子类修改topcode失败!');
            }

            if(res === false)
            {
              $response->alert('修改失败');
              $response->script('self.location=self.location');
            }
            else
            {
              $response->alert('修改成功');
              $response->script("javascript:history.go(-1)");
            }
            $response->execute();
        }
        else
        {
            $where = "tcode = '".$tcode."'";
            $res = $typeobj->get_one($where);
            $options = $typeobj->alltype();
            include template('admin', 'type_edit');
        }
        break;
    case 'delete':
        $have_perm = check_perms('TYPE004');
        if($have_perm == false)
        {
            showmessage('您没有权限进行此操作！');
        }
        if(isset($tcode))
        {
            $res = $typeobj->delete_types($tcode);
            if($res === true)
            {
                $response->alert('删除完毕');
            }
            else
            {
                $response->alert('删除失败');
            }
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert('操作失败');
            $response->script('self.location=self.location');
        }
                $response->execute();
        break;
    case 'listorder':
        if(isset($listorder))
        {
         $res = $typeobj->listorder($listorder);
             if($res === true)
             {
                 $response->alert('修改成功');
                 $response->script('self.location=self.location');
             }
             else
             {
                 $response->alert('操作失败');
                 $response->script('self.location=self.location');
             }
        }
        else
        {
            $response->script('self.location=self.location');
        }

                $response->execute();
         break;
    default:
        if(check_perms('TYPE001') === false)
        {
            showmessage('您没有此项操作权限！');
        }
        $page = isset($page) ? intval($page) : 1;
        $page = max($page, 1);
        $pagesize = 10;
        $res = $typeobj->count($parentcode);
        $count=isset($res['total']) ? intval($res['total']) : 0;
        $type_res = $typeobj->list_info($page, $pagesize, $parentcode);
        $l = $typeobj->get_one("tcode = '".$parentcode."'");
// 给添加分类链接 赋值topcode
        if($l != false)
            {
                if($l['topcode'] == '')
                 {
                    $tcode = $l['tcode'];
                 }
                else
                 {
                    $tcode = $l['topcode'];
                 }
            }
        $back_url = make_forward();
        $pages=pages($count, $page, $pagesize);
        include template('admin', 'type_manage');
}
?>
